import { Nodes } from '@knno/dom';
import { ColumnOption, List as UIList, ListEventMap, SelectType } from '../components/list/list';
import { JSXProps } from '@knno/dom/jsx';
import { ColumnSizeType, RowType, Section, SortType } from '../components/grid/gridBase';
import { AttrCheck, isArray, isBool, isNumber, isObject, isStr, jsxBuild, On } from './utils';
import { GridConfig } from '../components/grid/grid';

export { UIList };

interface ListProps extends JSXProps<UIList>, On<ListEventMap, UIList> {
	config?: Partial<GridConfig>;
	autoHeight?: boolean;
	columnBorder?: boolean;
	columnSize?: ColumnSizeType;
	column?: ColumnOption;
	headLine?: boolean;
	sortType?: SortType;
	selectable?: boolean;
	selected?: RowType[];
	activable?: boolean;
	rows?: RowType[];
	showEmpty?: boolean;
	fixColumns?: number;
	section?: Section | null;
	selectType?: SelectType;
	disableOn?: string | number | null;
}

const ATTRS: AttrCheck = {
	config: isObject,
	autoHeight: isBool,
	columnBorder: isBool,
	columnSize: isStr,
	column: isObject,
	headLine: isBool,
	sortType: isStr,
	selectable: isBool,
	selected: isArray,
	activable: isBool,
	rows: isArray,
	showEmpty: isBool,
	fixColumns: isNumber,
	section: (v) => isObject(v) || v === null,
	selectType: isStr,
	disableOn: (v) => isStr(v) || isNumber(v) || v === null,
};

export function List(props: ListProps, children: Nodes[]): UIList {
	return jsxBuild(new UIList(), props, children, ATTRS);
}
